from django.shortcuts import render
from rest_framework.parsers import JSONParser
from .models import Publisher, Book, author
from rest_framework.decorators import api_view
from django.http import HttpResponse, JsonResponse  # 返回状态


# Create your views here.
@api_view(['post'])
def add_Publisher(request):
    data = JSONParser().parse(request)
    name = data.get('name')
    address = data.get('address')
    Publisher(name=name, address=address).save()
    return JsonResponse({
        'code': 200,
        'msg': "添加成功"
    })

@api_view(['post'])
def add_Book(request):
    data = JSONParser().parse(request)
    book_name = data.get('book_name')
    price = data.get('price')
    ku_cun = data.get('ku_cun')
    mai = data.get('mai')
    publisher_name = data.get('publisher_name')
    Book(book_name=book_name, price=price, ku_cun=ku_cun, mai=mai, publisher_name=publisher_name).save()
    return JsonResponse({
        'code': 200,
        'msg': "添加成功"
    })

@api_view(['post'])
def add_author(request):
    data = JSONParser().parse(request)
    author_name = data.get('author_name')
    gender = data.get('gender')
    age = data.get('age')
    book_name = data.get('book_name')
    author(author_name=author_name, gender=gender, age=age, book_name=book_name).save()
    return JsonResponse({
        'code': 200,
        'msg': "添加成功"
    })
